package com.stock.listener;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.stock.service.StockService;

public class StockListener implements ServletContextListener {
	public final static Logger log = LogManager.getLogger(StockListener.class);
			
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		 
	}
	
public static void main(String s[]) {
	 Calendar date1 = Calendar.getInstance();
	 if (date1.get(Calendar.HOUR_OF_DAY) > 10) date1.add(Calendar.DATE, 1);
	 
	 
	 System.out.println(date1.getTime());
}
	
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		 log.info("Start setting stock time task  job");
		
		 Calendar date1 = Calendar.getInstance();
		 if (date1.get(Calendar.HOUR_OF_DAY) > 10) date1.add(Calendar.DATE, 1);
		 date1.set(Calendar.HOUR_OF_DAY, 10);
		 date1.set(Calendar.MINUTE, 0);
		 date1.set(Calendar.SECOND, 0);
		
		 Calendar date2 = Calendar.getInstance();
		 if (date2.get(Calendar.HOUR_OF_DAY) > 11) date2.add(Calendar.DATE, 1);
		 date2.set(Calendar.HOUR_OF_DAY, 11);
		 date2.set(Calendar.MINUTE, 0);
		 date2.set(Calendar.SECOND, 0);
		 
		 Calendar date3 = Calendar.getInstance();
		 if (date3.get(Calendar.HOUR_OF_DAY) > 12) date3.add(Calendar.DATE, 1);
		 date3.set(Calendar.HOUR_OF_DAY, 12);
		 date3.set(Calendar.MINUTE, 0);
		 date3.set(Calendar.SECOND, 0);
		 
		 Calendar date4 = Calendar.getInstance();
		 if (date4.get(Calendar.HOUR_OF_DAY) > 13) date4.add(Calendar.DATE, 1);
		 date4.set(Calendar.HOUR_OF_DAY, 13);
		 date4.set(Calendar.MINUTE, 0);
		 date4.set(Calendar.SECOND, 0);
		 
		 Calendar date5 = Calendar.getInstance();
		 if (date5.get(Calendar.HOUR_OF_DAY) >= 14) date5.add(Calendar.DATE, 1);
		 date5.set(Calendar.HOUR_OF_DAY, 13);
		 date5.set(Calendar.MINUTE, 31);
		 date5.set(Calendar.SECOND, 0);
		 
		 log.info("job1 run time :" + DateFormatUtils.format(date1,  "yyyy/MM/dd HH:mm:ss"));
		 log.info("job2 run time :" + DateFormatUtils.format(date2, "yyyy/MM/dd HH:mm:ss"));
		 log.info("job3 run time :" + DateFormatUtils.format(date3, "yyyy/MM/dd HH:mm:ss"));
		 log.info("job4 run time :" + DateFormatUtils.format(date4, "yyyy/MM/dd HH:mm:ss"));
		 log.info("job5 run time :" + DateFormatUtils.format(date5, "yyyy/MM/dd HH:mm:ss"));
		 
		 //每天執行
		 long execDailyTime = 1000 * 60 * 60 * 24;
		 
		 Timer timer = new Timer();
		 timer.schedule(new StockTimeTask(date1), date1.getTime(), execDailyTime);
		 timer.schedule(new StockTimeTask(date2), date2.getTime(), execDailyTime);
		 timer.schedule(new StockTimeTask(date3), date3.getTime(), execDailyTime);
		 timer.schedule(new StockTimeTask(date4), date4.getTime(), execDailyTime);
		 timer.schedule(new StockTimeTask(date5), date5.getTime(), execDailyTime);
		 
		 log.info("End setting stock time task job");
	}
}

class StockTimeTask extends TimerTask{
	Calendar date ;
	public StockTimeTask(Calendar date) {
		this.date = date;
	}
	
	@Override
	public void run() {
		StopWatch watch = new StopWatch();
		watch.start();
		
		StockListener.log.info("Start run [" + DateFormatUtils.format(date.getTime(), "yyyy/MM/dd HH:mm:ss") +"] StockTimeTask");
		StockService  stockService = new StockService();
		try {
			stockService.computeTodayKeyPoing();
		} catch (Exception e) {
			e.printStackTrace();
			StockListener.log.error(e.getMessage(), e);
		}
		watch.stop();
		StockListener.log.info("End run StockTimeTask runtime :" + watch.getTime() / 1000 + "ms" );
	}
	
}

 